using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Core
{
    [XmlRoot("helpCollection")]
    public class HCManifest : ISavable
    {
        private string _filePath = string.Empty;

        [XmlIgnore]
        public string FilePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }

        public void SaveToDisk()
        {
            using (StreamWriter sw = new StreamWriter(FilePath, false))
            {
                string xml = Utils.Serialize(this, typeof(HCManifest));
                sw.WriteLine(xml);
            }
        }

        private string _name = string.Empty;

        public string name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _id = string.Empty;

        public string id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _changeDate = string.Empty;

        public string changeDate
        {
            get { return DateTime.Now.ToString(Utils.DEFAULT_DATETIME_FORMAT_STRNG); }
            set { _changeDate = value; }
        }

        private string _createdDate = string.Empty;

        public string createdDate
        {
            get { return DateTime.Now.ToString(Utils.DEFAULT_DATETIME_FORMAT_STRNG); }
            set { _createdDate = value; }
        }

        private string _author = string.Empty;

        public string author
        {
            get { return _author; }
            set { _author = value; }
        }

        private string _version = string.Empty;

        public string version
        {
            get { return _version; }
            set { _version = value; }
        }

        private string _lcid = string.Empty;

        public string lcid
        {
            get { return _lcid; }
            set { _lcid = value; }
        }
        
        private string _defaultHelpItemId = string.Empty;

        public string defaultHelpItem
        {
            get { return _defaultHelpItemId; }
            set { _defaultHelpItemId = value; }
        }

        private string _brandingImage = string.Empty;

        public string brandingImage
        {
            get { return _brandingImage; }
            set { _brandingImage = value; }
        }

        private string _rootCategoryId = string.Empty;

        public string rootCategory
        {
            get { return _rootCategoryId; }
            set { _rootCategoryId = value; }
        }
        
        private Feedback _feedback = new Feedback();

        public Feedback feedback
        {
            get { return _feedback; }
            set { _feedback = value; }
        }

        private List<HelpItemManifestEntry> _helpItems = new List<HelpItemManifestEntry>();

        [XmlArray("helpItems"), XmlArrayItem("helpItem")]
        public List<HelpItemManifestEntry> helpItemEntries
        {
            get { 
                return _helpItems; }
            set { _helpItems = value; }
        }

        private List<HelpItemManifestEntryReferencedCollectionEntry> _referencedCollections = null;

        [XmlArray("referencedCollections"), XmlArrayItem("collection")]
        public List<HelpItemManifestEntryReferencedCollectionEntry> referencedCollections
        {
            get { return _referencedCollections; }
            set { _referencedCollections = value; }
        }
    }

    public class Feedback
    {
        private string _show = string.Empty;

        private bool _showBool = false;

        [XmlIgnore]
        public bool ShowBool
        {
            get { return _showBool; }
            set { _showBool = value; }
        }

        [XmlAttribute("show")]
        public string Show
        {
            get 
            {
                return _showBool ? "True" : "False";
            }
            set 
            {
                ShowBool = Boolean.Parse(value);
            }
        }
    }

    public class HelpItemManifestEntryReferencedCollectionEntry
    {
        private int _sortOrder = -1;

        [XmlAttribute("sortOrder")]
        public int SortOrder
        {
            get { return _sortOrder; }
            set { _sortOrder = value; }
        }

        private string _value = string.Empty;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public override string ToString()
        {
            return this.Value;
        }
    }

    public class HelpItemRelatedItemEntry
    {
        private int _sortOrder = -1;

        [XmlAttribute("sortOrder")]
        public int sortOrder
        {
            get { return _sortOrder; }
            set { _sortOrder = value; }
        }

        private string _value = string.Empty;

        [XmlTextAttribute()]
        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
            }
        }

        public override string ToString()
        {
            return string.Concat(this.Value, " sortOrder: ", this.sortOrder.ToString());
        }
    }

    public class HelpItemManifestEntryParent
    {
        private int _sortOrder = -1;

        [XmlAttribute("sortOrder")]
        public int sortOrder
        {
            get { return _sortOrder; }
            set { _sortOrder = value; }
        }

        private bool _primary = true;

        [XmlAttribute("primary")]
        public bool primary
        {
            get { return _primary; }
            set { _primary = value; }
        }

        private string _value = string.Empty;

        [XmlTextAttribute()]
        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
            }
        }

        public override string ToString()
        {
            return string.Concat(this.Value, " sort order: ", this.sortOrder.ToString());
        }
    }
}
